﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using snat.model;
using Microsoft.Win32;
using System.Runtime.InteropServices;
using System.Diagnostics;

namespace gui
{
    /// <summary>
    /// Interaction logic for Menu.xaml.
    /// The main program menu bar
    /// </summary>
    public partial class Menu : UserControl {

        public static event guiSaveJPGEvent OnSaveJPGEvent;
        public static event guiSaveChartJPGEvent OnSaveChartJPGEvent;
        public static event guiSaveChartCSVEvent OnSaveChartCSVEvent;
        public static event guiResetEvent OnResetEvent;

        public Menu() {
            InitializeComponent();
        }

        /* FILE MENU OPTIONS */
        private void OpenConnectBox(object sender, RoutedEventArgs e) {
            ConnectBox box = new ConnectBox();
            box.Show();
        }

        private void SaveGML(object sender, RoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".gml";
            dlg.Filter = "GraphML documents (.gml)|*.gml";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            Graph.SaveGML(filename);
        }

        private void SaveJPG(object sender, RoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "JPEG documents (.jpeg)|*.jpg";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            if (OnSaveJPGEvent != null && filename != "" && filename != String.Empty) OnSaveJPGEvent(filename);
        }

        private void SaveChartCSV(object sender, RoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".csv";
            dlg.Filter = "CSV documents (.csv)|*.csv";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            if (OnSaveChartCSVEvent != null && filename != "" && filename != String.Empty) OnSaveChartCSVEvent(filename);
        }

        private void SaveChartJPG(object sender, RoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.DefaultExt = ".jpg";
            dlg.Filter = "JPEG documents (.jpeg)|*.jpg";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            if (OnSaveChartJPGEvent != null && filename != String.Empty) OnSaveChartJPGEvent(filename);
        }


        /* ALGORITHMS MENU OPTIONS */
        private void OpenAlgorithmsBox(object sender, RoutedEventArgs e) {
            if (!NetworkModel.Instance.TapeHasLoaded) {
                InitialiseBox box = new InitialiseBox();
                box.Show();
            } else {
                MessageBox.Show("As the Network Model is already initialised, you cannot register new algorithms in this manner. To register or deregister algorithms from their execution, select to enable/disable them from the Network Statistics tab.", "Cannot Register New Algorithms", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        // Save full console output to file
        private void SaveConsoleOutput(object sender, RoutedEventArgs e) {
            string filename = "";
            SaveFileDialog dlg = new SaveFileDialog();
            dlg.Title = "Select directory for algorithm output files";
            dlg.DefaultExt = ".txt";
            dlg.Filter = "Text documents (.txt)|*.txt";
            if (dlg.ShowDialog() == true) {
                filename = dlg.FileName;
            }
            AlgorithmsTab.SaveConsoleOutput(filename);
        }

        // Save all algorithm console output to individual files
        private void SaveAllAlgorithmConsoleOutput(object sender, RoutedEventArgs e) {
            // Have the user specify the output folder
            System.Windows.Forms.FolderBrowserDialog dialog = new System.Windows.Forms.FolderBrowserDialog();
            dialog.ShowDialog();
            string directory = dialog.SelectedPath;
            if(directory != null && directory != String.Empty) AlgorithmsTab.SaveAlgorithmConsoleOutput(directory);
        }

        // Save algorithm console output to file
        private void SaveAlgorithmConsoleOutput(object sender, RoutedEventArgs e) {
            AlgorithmsTab.CallSaveAlgorithmConsoleOutput();
        }

        // Custom Algorithm Write
        private void WriteAlgorithmOutput(object sender, RoutedEventArgs e) {
            AlgorithmsTab.CallWriteAlgorithmCustomOutput();
        }

        // Custom Algorithm Read
        private void ReadAlgorithmInput(object sender, RoutedEventArgs e) {
            AlgorithmsTab.CallReadAlgorithmCustomFile();
        }

        // Reset NetworkModel
        private void ResetAll(object sender, RoutedEventArgs e) {
            if (NetworkModel.Instance.TapeHasLoaded) {
                MessageBoxResult answer = MessageBox.Show("Are you sure you want to reset Network Model? All edges will be removed and the message iterator will be reset.", "Reset Network Model?", MessageBoxButton.YesNo, MessageBoxImage.Question);
                if (answer == MessageBoxResult.Yes) {
                    NetworkModelConnect.Instance.Reset();
                    if (OnResetEvent != null) OnResetEvent();
                }
            } else {
                MessageBox.Show("As the Network Model is not yet initialised, there is nothing to reset.", "Network Model Ready", MessageBoxButton.OK, MessageBoxImage.Exclamation);
            }
        }

        private void Close(object sender, RoutedEventArgs e) {
            MessageBoxResult answer = MessageBox.Show("Are you sure you want to exit? Any unsaved results will be lost.", "Close NAV", MessageBoxButton.YesNo, MessageBoxImage.Question);
            if (answer == MessageBoxResult.Yes) Application.Current.Shutdown();
        }

        private void AboutNAV(object sender, RoutedEventArgs e) {
            AboutBox box = new AboutBox();
            box.Show();
        }

        private void ViewDoc(object sender, RoutedEventArgs e) {
            // Get current application path (and remove the "file://" portion)
            string path = System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase.Substring(8);
            path = path.Substring(0, path.Length-4 -(System.Reflection.Assembly.GetExecutingAssembly().GetName().Name).Length);
            
            // Open Windows Explorer at the correct path for the PDF Documentation
            Process.Start(path);
        }

    }
}
